package com.wnhz.wnmap.smovie.genre.util;

import com.wnhz.wnmap.domain.entity.Genre;
import javafx.concurrent.Worker;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xml.security.Init;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

@Slf4j
public class PoiUtil {

    private XSSFWorkbook workbook;
    private static PoiUtil instance = new PoiUtil();

    public static PoiUtil getInstance() {
        return instance;
    }

    public void init(InputStream inputStream) {
        try {
            workbook = new XSSFWorkbook(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SneakyThrows
    public List parseExcel(Class<?> clazz) {
        XSSFSheet sheet = workbook.getSheet("genre");

        log.debug("最后一行位置: {}", sheet.getLastRowNum());
        log.debug("最后一列位置: {}", sheet.getRow(1).getLastCellNum());

        int lastRow = sheet.getLastRowNum();
        XSSFRow headRow = sheet.getRow(0);
        Properties properties = new Properties();
        for (int i = 0; i < headRow.getLastCellNum(); i++) {
            properties.setProperty(String.valueOf(i), headRow.getCell(i).getStringCellValue());
        }
        log.debug("解析excel表头成功:{}", properties);

        List list = new ArrayList<>(50);
        int rowIndex = 1;
        while (rowIndex <= lastRow) {   //扫描一行
            Object obj = clazz.newInstance();
            XSSFRow row = sheet.getRow(rowIndex++);
            for (int column = 0; column < row.getLastCellNum(); column++) {

                XSSFCell cell = row.getCell(column);
                String methodName = "set" + properties.get(String.valueOf(column));

                switch (cell.getCellType()) {
                    case STRING:
                        Genre.class
                                .getDeclaredMethod(methodName, String.class)
                                .invoke(obj, cell.getStringCellValue());
                        break;
                    case NUMERIC:
                        if (DateUtil.isCellDateFormatted(cell)) {
                            Genre.class.getDeclaredMethod(methodName, Date.class)
                                    .invoke(obj, cell.getDateCellValue());
                        } else {
                            Genre.class.getDeclaredMethod(methodName, double.class)
                                    .invoke(obj, cell.getNumericCellValue());
                        }
                        break;
                    default:
                }
            }
            list.add(obj);
        }
        log.debug("创建对象数量:{}", list.size());
        return list;
    }


}
